App.MainApp.module('layoutModule', function(layoutModule){
	var _modulename = this.moduleName;
	this.startWithParent = false;
	// private
	var _layout = null;
	// View for Layout
   var MainView = Marionette.ItemView.extend({
		tagName: 'div',
		initialize: function() {}
   });
	// Controller for Layout
	var Controller = Marionette.Controller.extend({
		initialize: function(options){
			if (App.debug) console.log(this.moduleName + ' Controller initialized');
			this.region = options.region
		},
		show: function(options) {
			if (App.debug) console.log(this.moduleName + ' Controller show invoked');
			var main_view = new MainView({
				id: options.id,
				template: '#' + options.template
			});
			this.region.show(main_view);
		},
		render: function(options) {
			var self = this;
			if (App.debug) console.log(this.moduleName + ' Controller show invoked');
			_layout = $('#' + options.id).layout(options.layout);
			_layout.east.options.onclose_end   			= function(pane) { self.close_end(pane); }
			_layout.east.options.onclose_start 			= function(pane) { self.close_end(pane); }
			_layout.east.options.onopen_end    			= function(pane) { self.open_end(pane); }
			_layout.east.options.onopen_start  			= function(pane) { self.open_start(pane); }
			_layout.east.options.onresize_end    		= function(pane) { self.resize_end(pane); }
			_layout.east.options.onresize_start  		= function(pane) { self.resize_start(pane); }
			_layout.east.options.livePaneResizing   	= function(pane) { self.close_end(pane); }
		},
		close_end: function(pane) {
			App.MainApp.vent.trigger(_modulename + ":resized");
		},
		close_start: function(pane) {
			//App.MainApp.vent.trigger("layout:resized");
		},
		open_end: function(pane) {
			App.MainApp.vent.trigger(_modulename + ":resized");
		},
		open_start: function(pane) {
			//App.MainApp.vent.trigger("layout:resized");
		},
		resize_end: function(pane) {
			App.MainApp.vent.trigger(_modulename + ":resized");
		},
		resize_start: function(pane) {
			//App.MainApp.vent.trigger("layout:resized");
		}
	});
	
	layoutModule.addInitializer(function (options) {
		var defaults = {
			id: 'main-layout',
			layout: {
				defaults: {
					fxName:               'slide',
					fxSpeed:              'slow',
					spacing_closed:       '5',
					spacing_open:         '5',
					initClosed:           false,
					showDebugMessages:	 true,
					livePaneResizing:		 true
				}
			},
			region: App.MainApp.mainRegion
		};
		options = _.extend(defaults, options);
		if (App.debug) console.log('App.layoutModule addInitializer');
		layoutModule.Controller = new Controller({
			region: options.region
		});
		layoutModule.Controller.show(options);
		layoutModule.Controller.render(options);
   });
});

App.MainApp.layoutModule.on("before:start", function(){
	if (App.debug) console.log(this.moduleName + ' before:start');
});

App.MainApp.layoutModule.on("start", function(){
	if (App.debug) console.log(this.moduleName + ' start');
});

